import { Logger } from "../../engine/Logger";
import { GameConfig } from "../config/GameConfig";
import { RoomModel } from "./RoomModel";
import { UserModel } from "./UserModel";

export class RoomPlayer {

    public userid: number;
    public nick_name: string;
    public headimg: string;
    public img1: string;
    public img2: string;
    public lv: number;
    public invite: number
    public score: number = 0
    public chair: number = 0
    public ready: boolean = false;
    public joinTime: number = 0
    public sex: number = 0
    public location: string = ""
    public ip: string;
    public matchTurn: number = 0
    public max_score: number = 0
    public chargeEndTime: number = 0 //充值限时

    public showImgIndex: number = 0
    public isOnline: boolean = true
    public isGetInfo:boolean = false

    public zimo:number = 0      //自摸次数
    public jiepao:number = 0    ////接炮次数
    public dianpao:number = 0   //点炮次数
    public gangTimes:number = 0 //摸豆次数
    public jiTimes:number = 0//摸鸡次数

    public static createPlayerByObj(obj: object) {
        let player: RoomPlayer = new RoomPlayer()
        player.userid = obj["userid"]
        player.nick_name = obj["nick_name"]
        player.headimg = obj["headimg"]
        player.img1 = obj["img1"]
        player.img2 = obj["img2"]
        player.lv = obj["lv"]
        player.invite = obj["invite"]
        player.score = obj["score"]
        player.chair = obj["chair"]
        player.ready = obj["ready"]
        player.joinTime = obj["joinTime"]
        player.sex = obj["sex"]
        player.location = obj["location"]
        player.ip = obj["ip"]
        player.matchTurn = obj["matchTurn"]
        player.max_score = obj["max_score"]
        player.chargeEndTime = obj["chargeEndTime"]
        player.chargeEndTime = obj["chargeEndTime"]
        player.isOnline = obj["isOnline"]
        player.zimo = obj["zimo"]
        player.jiepao = obj["jiepao"]
        player.dianpao = obj["dianpao"]
        player.gangTimes = obj["gangTimes"]
        player.jiTimes = obj["jiTimes"]
        return player
    }

    public static getMyPlayer(): RoomPlayer {
        // Logger.log("getMyPlayer==", GameModel.instance.isPlayRecord, GuanDanVideoInfo.instance.watchChairId, this.playerList)
        for (let i = 0; i < RoomModel.instance.playerList.length; i++) {
            let player: RoomPlayer = RoomModel.instance.playerList[i];
            if (player) {
                if (RoomModel.instance.isPlayVideo) {
                    if (player.chair == RoomModel.instance.watchChairId) {
                        return player;
                    }
                }
                if (player.userid == UserModel.instance.userId) {
                    return player;
                }
            }
        }
    }

    public static getPlayerBySeat(seat: number, seatNum: number) {
        let myPlayer: RoomPlayer = this.getMyPlayer()
        let chair: number = (myPlayer.chair - 1 + seat) % seatNum + 1
        if (chair % seatNum == 0) {
            chair = 0
        }
        let player: RoomPlayer = this.getPlayerByChair(chair)
        // Logger.log("getPlayerBySeat====", seat, myPlayer, seatNum, chair, player)
        return player;
    }

    public static getPlayerByChair(chairId: number): RoomPlayer {
        for (let i = 0; i < RoomModel.instance.playerList.length; i++) {
            let player: RoomPlayer = RoomModel.instance.playerList[i];
            if (player && player.chair == chairId) {
                return player;
            }
        }
    }

    public static getPlayerByUserId(userid: number) {
        for (let i = 0; i < RoomModel.instance.playerList.length; i++) {
            let player: RoomPlayer = RoomModel.instance.playerList[i];
            if (player && player.userid == userid) {
                return player;
            }
        }
    }

    //根据服务器的座位号得到客户端的座位号  逆时针
    public static getSeatByChair_ni(chairId: number, playerNum: number = RoomModel.instance.playerList.length) {
        let myPlayer: RoomPlayer = this.getMyPlayer();
        let seat: number = (chairId - myPlayer.chair + playerNum) % playerNum
        if (RoomModel.instance.gameType == GameConfig.Game_Type.huanghua_mj
            || RoomModel.instance.gameType == GameConfig.Game_Type.cangxian_mj
            || RoomModel.instance.gameType == GameConfig.Game_Type.dazhonger_mj) {
            if (RoomModel.instance.conf.renshu == 2) {
                if (seat == 1) {
                    seat = 2
                }
            }
            else if (RoomModel.instance.conf.renshu == 3) {
                if (seat == 2) {
                    seat = 3
                }
            }
        }
        return seat;
    }

    //顺时针时返回玩家位置
    public static getSeatByChair_shun(chairId: number, playerNum: number = RoomModel.instance.playerList.length) {
        let myPlayer: RoomPlayer = this.getMyPlayer();
        let seat: number = (myPlayer.chair - chairId + playerNum) % playerNum;
        return seat;
    }


    public static getPlayerNum(playerList: Array<RoomPlayer>) {
        let count: number = 0
        for (let i = 0; i < playerList.length; i++) {
            if (playerList[i]) {
                count++
            }
        }
        return count

    }


}